#!/bin/bash
# $Id: echo_end,v 1.9 2004/10/06 20:20:27 repeater Exp $
# $Log: echo_end,v $
# Revision 1.10  2009/9/24 20:20:27  repeater
# Use disconnect all instead of disconnect $NODE_CALL 
#
# Revision 1.9  2004/10/06 20:20:27  repeater
# Possible work around for timeout audo message.
#
# Revision 1.8  2004/09/01 14:01:15  repeater
# Fix to either run custom_off or echo_status so echostatus is only run once.
#
# Revision 1.7  2004/08/29 21:57:54  repeater
# Correct to add > to one of the &>/dev/null redirects for a tbdcmd.
#
# Revision 1.6  2004/08/29 17:00:37  repeater
# Hide output from tbdcmd.
#
# Revision 1.5  2004/08/25 00:04:08  repeater
# Removed echolinkoff from echo_wavplay announcements.
#
# Revision 1.4  2004/08/19 05:45:52  repeater
# Change the disconnect audio logic to match echo_inbound and echo_call.
#
# Revision 1.3  2004/08/19 04:28:50  repeater
# Changed the disconnect audio to include the standard hi-low tones.
#
# Revision 1.2  2004/08/17 04:44:44  repeater
# Add monitor disable to disconnect.
#
# Revision 1.1  2004/08/17 04:09:27  repeater
# Initial revision
#
###########################################################
#                                                         #                  
#  Released under the GNU Public License                  #                  
#                                                         #                  
#  Script :  echo_end                                     #
#  Author :  Marcus Xenakis WA2DCI                        #
#  Version:  .15 Beta                                     #
#  Date   :  3/22/03                                      #
#  About  :  This script tests to see if the last call    #
#            was EchoLink or IRLP. Passes to normal IRLP  #
#            "end" script if not an EchoLink call         #
#  CHANGELOG:                                             #
#  06-Mar-04 VK2XJG: Modification for status updates      #
#                                                         #
###########################################################

[ -z "$ECHO_SCRIPT" ] && ECHO_SCRIPT=/home/EchoIRLP/scripts
   source $ECHO_SCRIPT/echo_common

# Abort if no connection in progress to prevent killing echo-idle
if [ -f "$LOCAL"/echoidle ]; then
  # EchoIRLP is active, need to check if connection in place
  if [ ! -f "$LOCAL"/active ]; then 
    echo "${0##*/:} Restoring EchoIdle connection."
    # No official connection, check that imike and ispeaker are running
    # then abort
    "$ECHO_SCRIPT"/sfswrapper
    killall imike
    # Set Environment Variables for Speak Freely
    export SPEAKFREE_CNAME="CALLSIGN" 
    export SPEAKFREE_ID="$ECHO_NODE_DESC"::"$ECHO_TBD_PASSWD"

    IMIKEFLAG="-t"

    IMIKEFLAG3="$ECHO_TBD_HOST":"$ECHO_TBD_LISTEN"
    if [ "$FORCE_8BIT" = "YES" ] ; then
      "$BIN"/imike -8 "$IMIKEFLAG" $IMIKEFLAG2 $IMIKEFLAG3 &>/dev/null &
    else
      "$BIN"/imike "$IMIKEFLAG" $IMIKEFLAG2 $IMIKEFLAG3 &>/dev/null &
    fi
  exit 0
  fi
fi

# I not an EchoLink connection call normal IRLP end
if [ ! -f "$LOCAL"/echo_active ] ; then 
  "$SCRIPT"/end &
  exit 0
fi  

# Check that disconnect not already in progress
if [ -f "$LOCAL"/echo_disconnecting ] ; then
  echo "${0##*/}: Disconnect already in progress, aborting..."
  exit 0
fi

# Set "in progress" flag
touch $LOCAL/echo_disconnecting

if [ -f "$CUSTOM/timeoutvalue.temp" ] ; then    #KF7FLY
  mv -f "$CUSTOM/timeoutvalue.temp" "$CUSTOM/timeoutvalue" &>/dev/null
  killall dtmf &>/dev/null
  "$CUSTOM"/rc.mixer &>/dev/null
  dtmf &>/dev/null
fi      #KF7FLY

killall imike &>/dev/null
killall ispeaker &>/dev/null

#$BIN/key

"$SCRIPT"/fifoecho "$STATIONID" disconnect
"$SCRIPT"/dropcall

NODE_CALL=`cat "$LOCAL"/echo_call`
echo "${0##*/}: $NODE_CALL"

#LOGS the disconnect 
log "Node Disconnect from EchoLink `cat $LOCAL/echo_active` `cat $LOCAL/echo_call`"

if [ -f "$LOCAL"/echoirlp ] ; then
  # Disconnect from destination node
  TBD_COMMAND -q monitor disable $NODE_CALL &>/dev/null
  TBD_COMMAND -q disconnect all &>/dev/null
  echo "${0##*/}: TBD_COMMAND" $NODE_CALL
  rm -f "$LOCAL"/echoirlp
else
  # Otherwise we're using a shared conference - update our status
  "$ECHO_SCRIPT"/echo_status 
fi

# Save Node numbers
NODE_ID=`cat "$LOCAL"/echo_active`
CONVERTED_STATIONID=`cat "$LOCAL"/active`
usleep 500000
#JUNK=`pgrep -xf "/bin/bash /home/irlp/scripts/off timeout"`
JUNK=`ps x | grep "/bin/bash /home/irlp/scripts/off timeout$"`
if [ "$JUNK" != "" ] ; then
   echo "$JUNK" &>/dev/null
elif [ "$ECHO_SAY_NODE" = "NO" ] ; then
   "$ECHO_SCRIPT"/echo_wavplay echo_off
else
   # callsing or node
  if [ "$ECHO_SAY_NODE" = "YES" ] ; then
     "$ECHO_SCRIPT"/echo_wavgen "$LOCAL"/echo_active
  else
     "$ECHO_SCRIPT"/echo_wavgen "$LOCAL"/echo_call
  fi
  "$ECHO_SCRIPT"/echo_wavplay echo_id echo_off
fi
# remove status flags
rm -f "$LOCAL"/echo_active &>/dev/null
rm -f "$LOCAL"/echo_call &>/dev/null
rm -f "$LOCAL"/codec &>/dev/null
rm -f "$LOCAL"/selectcodec &>/dev/null
rm -f "$LOCAL"/udp_port &>/dev/null
rm -f "$LOCAL"/preaccess &>/dev/null
rm -f "$LOCAL"/connectedip &>/dev/null
rm -f "$LOCAL"/calledip &>/dev/null
rm -f "$RUN"/* &>/dev/null
rm -f "$LOCAL"/active &>/dev/null
rm -f "$LOCAL"/node_id &>/dev/null
rm -f "$LOCAL"/echoirlp &>/dev/null

# Reset timeout
touch "$LOCAL"/timeout

#added for the really fast machines :)
sleep 1

if [ "$CUSTOM_OFF" = "YES" ] && [ -f "$CUSTOM"/custom_off ] ; then
  "$CUSTOM"/custom_off $1
else
  "$ECHO_SCRIPT"/echo_status &
fi

# Update IRLP status
"$SCRIPT"/statupdate &

# Update Status
#"$ECHO_SCRIPT"/echo_status &

#"$BIN"/unkey

# Clear disconnect lock
rm -f $LOCAL/echo_disconnecting

exit 0
